@extends('layouts.default')

@section('title', '平台学分')

@push('scripts')
<script>
    layui.use(['layer','form','table','laytpl','laydate','element','laypage'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var element = layui.element;
        var laypage = layui.laypage;

        var filters = {};
        var per_page = 10;
        var totalNum = 0;
        var UserId = "{{request()->get('user_id',0)}}";
        
        renderSelect();

        // laydate.render({
        //     elem: '#range-date'
        //     ,type: 'date'
        //     ,range: true
        // });

        // 搜索查询
        form.on('submit(queryBtn)', function(data) {
            var param = data.field;
            filters = param
            getList(1, per_page);
        });
        // 重置
        form.on('submit(resetBtn)', function(data) {
            filters = {};
            $('#qryForm')[0].reset();
            getList(1, per_page)
        });
        form.on('submit(exportBtn)', function(data) {
            var param = data.field;
            var para = {
                "search": param
            };
            
            // exportCredits(para, function(res) {
            //     if (res.code == '9') {
            //         layer.msg(res.msg, {icon: 5, shift: 6});
            //     } else {
            //         exportsDel('学分培训信息列表.xlsx', '/download/excel/'+res.data.file_name);
            //     }
            // });
        });
        // 获取页面数据
        function getList(page, limit) {
            if ($.isEmptyObject(filters)){
                let param = form.val("qryForm");
                filters = param;
            }
            
            filters.user_id = UserId;
            var para = {
                "page": page,
                "search": filters,
                "limit": limit
            };

            getPlatformUserCreditList(para, function(res){
                if(res.code === 1) {
                    if(totalNum != res.total) {
                        totalNum = res.total;
                        per_page = res.limit;
                        renderPageBar(page,per_page)
                    }

                    table.render({
                        data: res.data,
                        elem: '#tablelsit',
                        toolbar: '#toolbarHead', //开启头部工具栏，并为其绑定左侧模板
                        defaultToolbar: [],
                        cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                        limit: res.limit,
                        skin: 'nob',
                        cols: [[
                            {field: 'term', title: '时期', align: 'center'},
                            {field: 'course_name', title: '课程名称', align: 'center'},
                            {field: 'course_level', title: '课程级别', align: 'center'},
                            {field: 'score', title: '获得学分', align: 'center'},
                            {field: 'get_time', title: '获得时间', align: 'center'},
                            // {title: '操作', width: '20%', align: 'center', toolbar: '#opDemo', fixed:'right'}
                        ]],
                        id:"tableIns",
                        done: function(res, curr, count) {
                            setBtnPermission();
                            curr_page = page;
                        },
                        page: false,
                        loading: true
                    });
                } else {
                    layer.msg(res.msg, {time: 2000, icon: 5});
                }
            });
        }

        // 渲染页面
        function renderPageBar(page,limit) {
            laypage.render({
                elem: 'pageBar',
                count: totalNum, //数据总数，从服务端得到
                curr: page,
                first: '第一页',
                last: '最后一页',
                limit:limit,
                layout: ['limit','count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj, first) {
                    //首次不执行
                    if(!first) {
                        layer.closeAll();
                        getList(obj.curr,obj.limit)
                    }
                }
            });
        }

        //表格头工具栏事件
        table.on('toolbar(tablelsit)', function(obj){
            switch(obj.event){
                case 'export':    
                    // exportDiscipline({}, function(res) {
                    //     if (res.code == '9') {
                    //         layer.msg(res.msg, {icon: 5, shift: 6});
                    //     } else {
                    //         exportsDel('任教学科信息列表.xlsx', '/download/excel/'+res.data.file_name);
                    //     }
                    // });
                    break;
            };
        });
        //监听行工具事件
        table.on('tool(tablelsit)', function(obj){
            var data = obj.data;

            if(obj.event === 'del'){
                // layer.confirm('确定删除吗?', function(index){
                    // delAward({id: data.id}, function(res) {
                    //     if (res.code == '9') {
                    //         layer.msg(res.msg, {icon: 5, shift: 6});
                    //     } else {
                    //         layer.msg('删除成功', {icon: 6,time: 1000},function(){
                    //             obj.del();
                    //             layer.close(index);
                    //         });
                    //     }
                    // });
                // });
            }
        });
        
        function exportsDel(showName,fileName){
            window.location.href = "{{ route('download.FileDownloadDel') }}?filename="+showName+"&file_url="+fileName;
        }


        //下拉框初始化
        async function renderSelect(){
            // await renderTermSelect();
            getList(1, per_page);
        }

        //学期下拉框数据渲染
        function renderTermSelect() {
            return new Promise((resolve) => {
                getTermData({},function(res){
                    if(res.code == 1){
                        $('[data-role=termList]').empty().append('<option value="">请选择</option>');
                        for(var i in res.data){
                            $('[data-role=termList]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                        }
                                      
                        form.render('select');

                        resolve();
                    }
                });  
            });
        }

    });
</script>
@endpush

@section('content')
<style>
    
</style>

<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <!-- <form id="qryForm" class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline" >
                        <label class="layui-form-label">姓名：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="user_name" placeholder="请输入" class="layui-input" autocomplete="off"> 
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="button" lay-submit lay-filter="queryBtn" class="layui-btn layui-btn-normal">查询</button>
                        <button type="button" lay-submit lay-filter="resetBtn" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form> -->

        </div>
    </div>
</div>

<div class="layui-card">
    <script type="text/html" id="toolbarHead">
        <div class="list-head">
            <h3>用户平台学分查看</h3>
            <div class="layui-btn-container list-opt-btns"></div>
        </div>
    </script>
 
    <div class="layui-card-body">
        <table class="layui-hide" id="tablelsit" lay-filter="tablelsit" lay-data="{id: 'tablelsit'}"></table>
        <div class="pagebarbox" style="text-align: end"><div id="pageBar" class="pagebar"></div></div>
    </div>

    <script type="text/html" id="opDemo">
        <a btnPermission="287" class="layui-btn layui-btn-primary layui-border-orange layui-btn-xs" href="{{route('platform.infoView')}}?id=@{{d.user_id}}">查看</a>
    </script>
</div>
  
@endsection
